package com.pearl.jacksonspringboot.demo.dict;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.pearl.jacksonspringboot.demo.mask.MaskFromXToYJsonSerializer;
import com.pearl.jacksonspringboot.demo.mask.MaskFromXToYMask;

import java.io.IOException;

/**
 * @author TD
 * @version 1.0
 * @date 2024/4/23
 */
public class DictJsonSerializer extends StdSerializer<Object> {


    private CommonEnum commonEnum;

    protected DictJsonSerializer() {
        super(Object.class);
    }

    protected DictJsonSerializer( CommonEnum commonEnum) {
        super(Object.class);
        this.commonEnum = commonEnum;
    }

    /**
     * 序列化
     */
    @Override
    public void serialize(Object code, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        // 获取枚举对应的翻译值并写出
        String description =commonEnum.getDescription(Integer.parseInt(code.toString()));
        jsonGenerator.writeString(description);
    }
}